import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
      list:[],
      ind:"",
      ind2:""
  },
  mutations: {
    setlist(state,payload){
        state.list=payload.data
    },
    setadd(state,payload){
      let obj={
        id:new Date().getTime(),
        name:payload
      }
      console.log(obj);
      state.list.push(obj)
    },
    setxb(state,payload){
      state.ind=payload
    },
    setxg(state,payload){
      // console.log(payload,state.ind);
      state.list.forEach((item,index)=>{
        if(item.id==state.ind){
          item.name=payload
        }
      })
    },
    setdelind(state,payload){
      state.ind2=payload
    },
    setdel(state,payload){
      state.list.forEach((item,index)=>{
        if(item.id==state.ind2){
          state.list.splice(item,1)
        }
      })
    }
  },
  actions: {
      getlist({commit}){
        axios.get("/list").then(res=>{
          commit("setlist",res.data)
        })
      }
  },
  modules: {
  }
})
